#tag Class
Protected Class sjgResize
Inherits Canvas
	#tag Event
		Sub Paint(g As Graphics)
		  if mTheControl = Nil then Exit Sub
		  
		  Dim centreX As Integer = ( g.Width - handleSize ) / 2
		  Dim centreY As Integer = ( g.Height - handleSize ) / 2
		  Dim rightX As Integer = me.Width - handleSize
		  Dim bottomY As Integer = me.Height - handleSize
		  
		  //g.ForeColor = &cffffff
		  //g.FillRect( 0, 0, g.Width, g.Height )
		  g.ForeColor = TextColor
		  g.DrawRect( 0, 0, g.Width, g.Height )
		  
		  g.ForeColor = grabCentreColour
		  // topCentre
		  g.FillRect( centreX, 0, handleSize, handleSize )
		  //centreLeft
		  g.FillRect( 0, centreY, handleSize, handleSize )
		  //centreRight
		  g.FillRect( rightX, centreY, handleSize, handleSize )
		  // bottomCentre
		  g.FillRect( centreX, bottomY, handleSize, handleSize )
		  
		  //Draw corners over centres if any overlap
		  g.ForeColor = grabCornerColour
		  // topLeft
		  g.FillRect( 0, 0, handleSize, handleSize )
		  // topRight
		  g.FillRect( rightX, 0, handleSize, handleSize )
		  // bottomLeft
		  g.FillRect( 0, bottomY, handleSize, handleSize )
		  // bottomRight
		  g.FillRect( rightX, bottomY, handleSize, handleSize )
		  
		  
		End Sub
	#tag EndEvent

	#tag Event
		Function MouseDown(X As Integer, Y As Integer) As Boolean
		  if IsContextualClick then
		    me.Hide
		    Return False
		  end if
		  mHandleGrabbed = 0
		  saveHeight = me.Height
		  saveLeft = me.Left
		  saveTop = me.Top
		  saveWidth = me.Width
		  saveX = System.MouseX
		  savey = System.MouseY
		  saveMouseDown = True
		  
		  Dim ret As Boolean
		  if not setMouseCursor(X, Y) Then
		    me.MouseCursor = System.Cursors.ArrowAllDirections
		  end if
		  Return True
		End Function
	#tag EndEvent

	#tag Event
		Sub MouseUp(X As Integer, Y As Integer)
		  me.MouseCursor = System.Cursors.StandardPointer
		  saveMouseDown = False
		  setCtrlPosition
		End Sub
	#tag EndEvent

	#tag Event
		Sub MouseMove(X As Integer, Y As Integer)
		  if saveMouseDown = False Then
		    if not setMouseCursor( X, Y ) then
		      me.MouseCursor = System.Cursors.StandardPointer
		    end if
		  end if
		End Sub
	#tag EndEvent

	#tag Event
		Sub MouseDrag(X As Integer, Y As Integer)
		  dim Xdiff, Ydiff As Integer
		  xdiff = System.MouseX - saveX
		  ydiff = system.MouseY - saveY
		  
		  if Xdiff = 0 And Ydiff = 0 Then
		    exit sub
		  end if
		  
		  Dim newLeft, newTop, newWidth, newHeight As Integer
		  newLeft = saveLeft
		  newTop = saveTop
		  newWidth = saveWidth
		  newHeight = saveHeight
		  
		  If Xdiff <> 0 Then
		    Select case mHandleGrabbed
		    case kMove
		      newLeft = saveLeft + Xdiff
		    case kTopRight, kCentreRight, kBottomRight
		      newWidth = saveWidth + Xdiff
		      if newWidth > maxWidth Then newWidth = maxWidth
		      if newWidth < minWidth Then newWidth = minWidth
		    case kTopLeft, kCentreLeft, kBottomLeft
		      newWidth = saveWidth - Xdiff
		      If newWidth < minWidth Then
		        newWidth = minWidth
		        Xdiff = saveWidth - newWidth
		      elseif newWidth > maxWidth Then
		        newwidth = maxWidth
		        Xdiff = saveWidth - newWidth 
		      End If
		      newLeft = saveLeft + Xdiff
		    End Select
		  End If
		  
		  If Ydiff <> 0 Then
		    Select case mHandleGrabbed
		    case kMove
		      newTop = saveTop + Ydiff
		    case kBottomRight, kBottomCentre, kBottomLeft
		      newHeight = newHeight + Ydiff 
		      if newHeight > maxHeight then newHeight = maxHeight
		      if newHeight < minHeight Then newHeight = minHeight
		    case kTopLeft, kTopCentre, kTopRight
		      newHeight = newHeight - Ydiff
		      if newHeight < minHeight then
		        newHeight = minHeight
		        Ydiff = saveHeight - newHeight
		      ElseIf newHeight > maxHeight Then
		        newHeight = maxHeight
		        Ydiff = saveHeight - newHeight
		      end if
		      newTop = newTop + Ydiff
		    End Select
		  End If
		  
		  if newLeft <> saveLeft then
		    me.Left = newLeft
		  end if
		  if newTop <> saveTop then
		    me.Top = newTop
		  end if
		  if newWidth <> saveWidth then
		    me.Width = newWidth
		  end if
		  if newHeight <> me.Height then
		    me.Height = newHeight
		  end if
		  
		End Sub
	#tag EndEvent

	#tag Event
		Sub LostFocus()
		  me.Visible = False
		End Sub
	#tag EndEvent

	#tag Event
		Sub Open()
		  me.Visible = False
		End Sub
	#tag EndEvent


	#tag Method, Flags = &h21
		Private Sub setMePosition()
		  If mTheControl is nil Then exit Sub
		  
		  If me.Left <> mTheControl.Left Then
		    me.Left = mTheControl.Left
		  End If
		  If me.Top <> mTheControl.Top Then
		    me.Top = mTheControl.Top
		  End If
		  If me.Width <> mTheControl.Width Then
		    me.Width = mTheControl.Width
		  End If
		  If me.Height <> mTheControl.Height Then
		    me.Height = mTheControl.Height
		  End If
		  
		  me.Refresh
		  
		End Sub
	#tag EndMethod

	#tag Method, Flags = &h0
		Sub setControl(r As Rectcontrol)
		  if r <> Nil Then
		    mTheControl = r
		    if handleSize < 0 Then
		      handleSize = min(mTheControl.Height, mTheControl.Width) / 3
		      if handleSize > 10 Then
		        handleSize = 10
		      end if
		    end if
		    if minWidth < handleSize * 2 Then
		      minWidth = handleSize * 2
		    end if
		    if minHeight < handleSize * 2 Then
		      minHeight = handleSize * 2
		    end if
		    setMePosition
		    me.Visible = True
		  end if
		End Sub
	#tag EndMethod

	#tag Method, Flags = &h21
		Private Function setMouseCursor(X As Integer, Y As Integer) As Boolean
		  //Corners take precedence if centres overlap
		  Dim rightX As Integer = me.Width - handleSize
		  Dim bottomY As Integer = me.Height - handleSize
		  If x >= rightX and y >= bottomY Then
		    mHandleGrabbed = kBottomRight
		    me.MouseCursor = System.Cursors.ArrowNorthwestSoutheast
		    Return True
		  End If
		  If x >= rightX and y <= handleSize Then
		    mHandleGrabbed = kTopRight
		    me.MouseCursor = System.Cursors.ArrowNortheastSouthwest
		    Return True
		  End If
		  If x <= handleSize and y >= bottomY Then
		    mHandleGrabbed = kBottomLeft
		    me.MouseCursor = System.Cursors.ArrowNortheastSouthwest
		    Return True
		  End If
		  If x <= handleSize and y <= handleSize Then
		    mHandleGrabbed = kTopLeft
		    me.MouseCursor = System.Cursors.ArrowNorthwestSoutheast
		    Return True
		  End If
		  
		  Dim centreX As Integer = ( me.Width - handleSize ) / 2
		  If ( x >= centreX ) and (x <= centreX + handleSize) and (y >= bottomY) Then
		    mHandleGrabbed = kBottomCentre
		    me.MouseCursor = System.Cursors.ArrowNorthSouth
		    Return True
		  End If
		  
		  Dim centreY As Integer = ( me.Height - handleSize ) / 2
		  If x >= rightX and y >= centreY and y <=  handleSize + centreY Then
		    mHandleGrabbed = kCentreRight
		    me.MouseCursor = System.Cursors.ArrowEastWest
		    Return True
		  End If
		  If x <= handleSize and y >= centreY and y <=  handleSize + centreY Then
		    mHandleGrabbed = kCentreLeft
		    me.MouseCursor = System.Cursors.ArrowEastWest
		    Return True
		  End If
		  If ( x >= centreX ) and (x <= centreX + handleSize) and (y <= handleSize) Then
		    mHandleGrabbed = kTopCentre
		    me.MouseCursor = System.Cursors.ArrowNorthSouth
		    Return True
		  End If
		  Return False
		End Function
	#tag EndMethod

	#tag Method, Flags = &h21
		Private Sub setCtrlPosition()
		  If mTheControl is nil Then exit Sub
		  
		  Dim changed As Boolean
		  
		  If me.Left <> mTheControl.Left Then
		    mTheControl.Left = me.Left
		    changed = True
		  End If
		  If me.Top <> mTheControl.Top Then
		    mTheControl.Top = me.Top
		    changed = True
		  End If
		  If me.Width <> mTheControl.Width Then
		    mTheControl.Width = me.Width
		    changed = True
		  End If
		  If me.Height <> mTheControl.Height Then
		    mTheControl.Height = me.Height
		    changed = True
		  End If
		  
		  if changed Then
		    mTheControl.Refresh
		    me.Refresh
		  end if
		End Sub
	#tag EndMethod

	#tag Method, Flags = &h0
		Sub Hide()
		  me.Visible = False
		End Sub
	#tag EndMethod


	#tag Property, Flags = &h21
		Private mTheControl As RectControl
	#tag EndProperty

	#tag Property, Flags = &h0
		handleSize As Integer = -1
	#tag EndProperty

	#tag Property, Flags = &h0
		grabCornerColour As Color = &c000000
	#tag EndProperty

	#tag Property, Flags = &h0
		grabCentreColour As Color = &cff8080
	#tag EndProperty

	#tag Property, Flags = &h21
		Private saveTop As Integer
	#tag EndProperty

	#tag Property, Flags = &h21
		Private saveLeft As Integer
	#tag EndProperty

	#tag Property, Flags = &h21
		Private saveWidth As Integer
	#tag EndProperty

	#tag Property, Flags = &h21
		Private saveHeight As Integer
	#tag EndProperty

	#tag Property, Flags = &h21
		Private saveX As Integer
	#tag EndProperty

	#tag Property, Flags = &h21
		Private saveY As Integer
	#tag EndProperty

	#tag Property, Flags = &h21
		Private mHandleGrabbed As Integer
	#tag EndProperty

	#tag Property, Flags = &h21
		Private saveMouseDown As Boolean
	#tag EndProperty

	#tag Property, Flags = &h0
		minWidth As Integer = 20
	#tag EndProperty

	#tag Property, Flags = &h0
		minHeight As Integer = 20
	#tag EndProperty

	#tag Property, Flags = &h0
		maxHeight As Integer = 10000
	#tag EndProperty

	#tag Property, Flags = &h0
		maxWidth As Integer = 10000
	#tag EndProperty


	#tag Constant, Name = kTopCentre, Type = Double, Dynamic = False, Default = \"2", Scope = Private
	#tag EndConstant

	#tag Constant, Name = kMove, Type = Double, Dynamic = False, Default = \"0", Scope = Private
	#tag EndConstant

	#tag Constant, Name = kTopRight, Type = Double, Dynamic = False, Default = \"3", Scope = Private
	#tag EndConstant

	#tag Constant, Name = kTopLeft, Type = Double, Dynamic = False, Default = \"1", Scope = Private
	#tag EndConstant

	#tag Constant, Name = kCentreRight, Type = Double, Dynamic = False, Default = \"4", Scope = Private
	#tag EndConstant

	#tag Constant, Name = kBottomRight, Type = Double, Dynamic = False, Default = \"5", Scope = Private
	#tag EndConstant

	#tag Constant, Name = kBottomCentre, Type = Double, Dynamic = False, Default = \"6", Scope = Private
	#tag EndConstant

	#tag Constant, Name = kBottomLeft, Type = Double, Dynamic = False, Default = \"7", Scope = Private
	#tag EndConstant

	#tag Constant, Name = kCentreLeft, Type = Double, Dynamic = False, Default = \"8", Scope = Private
	#tag EndConstant


	#tag ViewBehavior
		#tag ViewProperty
			Name="TabIndex"
			Visible=true
			Group="Position"
			InitialValue="0"
			Type="Integer"
			InheritedFrom="Canvas"
		#tag EndViewProperty
		#tag ViewProperty
			Name="Name"
			Visible=true
			Group="ID"
			Type="String"
			InheritedFrom="Canvas"
		#tag EndViewProperty
		#tag ViewProperty
			Name="Index"
			Visible=true
			Group="ID"
			Type="Integer"
			InheritedFrom="Canvas"
		#tag EndViewProperty
		#tag ViewProperty
			Name="Super"
			Visible=true
			Group="ID"
			InheritedFrom="Canvas"
		#tag EndViewProperty
		#tag ViewProperty
			Name="Left"
			Visible=true
			Group="Position"
			Type="Integer"
			InheritedFrom="Canvas"
		#tag EndViewProperty
		#tag ViewProperty
			Name="Top"
			Visible=true
			Group="Position"
			Type="Integer"
			InheritedFrom="Canvas"
		#tag EndViewProperty
		#tag ViewProperty
			Name="Width"
			Visible=true
			Group="Position"
			InitialValue="100"
			Type="Integer"
			InheritedFrom="Canvas"
		#tag EndViewProperty
		#tag ViewProperty
			Name="Height"
			Visible=true
			Group="Position"
			InitialValue="100"
			Type="Integer"
			InheritedFrom="Canvas"
		#tag EndViewProperty
		#tag ViewProperty
			Name="LockLeft"
			Visible=true
			Group="Position"
			Type="Boolean"
			InheritedFrom="Canvas"
		#tag EndViewProperty
		#tag ViewProperty
			Name="LockTop"
			Visible=true
			Group="Position"
			Type="Boolean"
			InheritedFrom="Canvas"
		#tag EndViewProperty
		#tag ViewProperty
			Name="LockRight"
			Visible=true
			Group="Position"
			Type="Boolean"
			InheritedFrom="Canvas"
		#tag EndViewProperty
		#tag ViewProperty
			Name="LockBottom"
			Visible=true
			Group="Position"
			Type="Boolean"
			InheritedFrom="Canvas"
		#tag EndViewProperty
		#tag ViewProperty
			Name="TabPanelIndex"
			Group="Position"
			InitialValue="0"
			Type="Integer"
			InheritedFrom="Canvas"
		#tag EndViewProperty
		#tag ViewProperty
			Name="Visible"
			Visible=true
			Group="Appearance"
			InitialValue="True"
			Type="Boolean"
			InheritedFrom="Canvas"
		#tag EndViewProperty
		#tag ViewProperty
			Name="HelpTag"
			Visible=true
			Group="Appearance"
			Type="String"
			EditorType="MultiLineEditor"
			InheritedFrom="Canvas"
		#tag EndViewProperty
		#tag ViewProperty
			Name="AutoDeactivate"
			Visible=true
			Group="Appearance"
			InitialValue="True"
			Type="Boolean"
			InheritedFrom="Canvas"
		#tag EndViewProperty
		#tag ViewProperty
			Name="Enabled"
			Visible=true
			Group="Appearance"
			InitialValue="True"
			Type="Boolean"
			InheritedFrom="Canvas"
		#tag EndViewProperty
		#tag ViewProperty
			Name="UseFocusRing"
			Visible=true
			Group="Appearance"
			InitialValue="True"
			Type="Boolean"
			InheritedFrom="Canvas"
		#tag EndViewProperty
		#tag ViewProperty
			Name="Backdrop"
			Visible=true
			Group="Appearance"
			Type="Picture"
			EditorType="Picture"
			InheritedFrom="Canvas"
		#tag EndViewProperty
		#tag ViewProperty
			Name="AcceptFocus"
			Visible=true
			Group="Behavior"
			Type="Boolean"
			InheritedFrom="Canvas"
		#tag EndViewProperty
		#tag ViewProperty
			Name="AcceptTabs"
			Visible=true
			Group="Behavior"
			Type="Boolean"
			InheritedFrom="Canvas"
		#tag EndViewProperty
		#tag ViewProperty
			Name="EraseBackground"
			Visible=true
			Group="Behavior"
			InitialValue="True"
			Type="Boolean"
			InheritedFrom="Canvas"
		#tag EndViewProperty
		#tag ViewProperty
			Name="InitialParent"
			Group="Behavior"
			InheritedFrom="Canvas"
		#tag EndViewProperty
		#tag ViewProperty
			Name="handleSize"
			Visible=true
			Group="Behavior"
			InitialValue="-1"
			Type="Integer"
		#tag EndViewProperty
		#tag ViewProperty
			Name="grabCornerColour"
			Visible=true
			Group="Behavior"
			InitialValue="&c000000"
			Type="Color"
		#tag EndViewProperty
		#tag ViewProperty
			Name="grabCentreColour"
			Visible=true
			Group="Behavior"
			InitialValue="&cff8080"
			Type="Color"
		#tag EndViewProperty
		#tag ViewProperty
			Name="minWidth"
			Visible=true
			Group="Behavior"
			InitialValue="20"
			Type="Integer"
		#tag EndViewProperty
		#tag ViewProperty
			Name="minHeight"
			Visible=true
			Group="Behavior"
			InitialValue="20"
			Type="Integer"
		#tag EndViewProperty
		#tag ViewProperty
			Name="maxHeight"
			Visible=true
			Group="Behavior"
			InitialValue="10000"
			Type="Integer"
		#tag EndViewProperty
		#tag ViewProperty
			Name="maxWidth"
			Visible=true
			Group="Behavior"
			InitialValue="10000"
			Type="Integer"
		#tag EndViewProperty
	#tag EndViewBehavior
End Class
#tag EndClass
